﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace WisDom.Models
{
    /// <summary>
    /// 公共实体信息
    /// </summary>
    public abstract class BaseEntity
    {

        /// <summary>
        /// 创建日期
        /// </summary>
        public DateTime? CreateDateTime { get; set; }
        /// <summary>
        /// 创建用户编号
        /// </summary>
        public string CreateUserId { get; set; }
        /// <summary>
        /// 创建用户名称
        /// </summary>
        public string CreateUserName { get; set; }
        /// <summary>
        /// 修改日期
        /// </summary>
        public DateTime? ModifyDateTime { get; set; }
        /// <summary>
        /// 修改用户编号
        /// </summary>
        public string ModifyUserId { get; set; }
        /// <summary>
        /// 修改用户名称
        /// </summary>
        public string ModifyUserName { get; set; }

        public virtual void Create()
        {
            CalcPrimaryKey();
            CreateDateTime = DateTime.Now;
        }
        public virtual void Modify()
        {
            ModifyDateTime = DateTime.Now;
        }

        public abstract void CalcPrimaryKey();




        public static Dictionary<string, object> GetCacheProps(BaseEntity baseEntity)
        {
            Dictionary<string, object> _lstCacheProps = new Dictionary<string, object>();
            Type type = baseEntity.GetType(); 
            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetCustomAttribute(typeof(NotMappedAttribute)) == null) //对含有NotMappedAttribute的属性进行过滤
                { 
                    string name = prop.Name;
                    object value = prop.GetValue(baseEntity, null);
                    _lstCacheProps.Add(name, value);
                }
            }
            return _lstCacheProps;
        }
    }
}
